<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
  <head>
    <title>教材吧-学校搜索</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<%@ include file="/base/jsp/includeweb.jsp"%>
    <link type="text/css" rel="stylesheet" href="${path}/base/css/book.css"/>
  </head>
  
  <body>
  <%@ include file="/base/jsp/header.jsp"%>
  <%@ include file="/base/jsp/search-header.jsp"%>
  <div id="content-wrapper" class="content-wrapper clear">
     <div class="book-left clear">
         <%@ include file="/base/jsp/book-type-nav.jsp"%>
         <%@ include file="/base/jsp/hot-books.jsp"%>
     </div>
     
     <div class="book-types-content clear">
         <div class="search-school-nav clear">
            <div class="search-nav"></div>
            <div class="search-but">
                <!--
                <ul id="dep-input" class="search-bottom-input"><span class="major-text">院系</span><li>信管</li><li>电子科学与工程</li></ul>
                <input type="button" value="公共选修课" id="search-school-but" class="but-org"/>
                -->
            </div>
         </div>
         <div class="book-types-content-books clear">
          
         </div>
         <%@ include file="/base/jsp/page.jsp"%>
     </div>
  </div>
  <%@ include file="/base/jsp/footer.jsp"%>
  </body>
  <script type="text/javascript">
  var school = getParameter("school");
  var scode = getParameter("scode");
  var depart = getParameter("depart");
  var dcode = getParameter("dcode");
  var major = getParameter("major");
  var mcode = getParameter("mcode");
  var sid = getParameter("sid");
  var did = getParameter("did");
  var mid = getParameter("mid");
  var yearCode = getParameter("yearCode");
  
 // alert(getYearCodeName(yearCode))
  //填充数据
  if(isDefine(sid)){
     $("#school-input").val(school);
     $("#school-input").attr("code",scode);
     $("#school-input").attr("sid",sid);
     getDepartment(sid);
    
      
     //添加学校的导航块
      var nav = $('<a href="#" class=""></a>');
      var sep = $('<span class="separate">&gt;</span>');
      nav.attr("class","s-nav");
      nav.text(school);
      
      $(".search-nav").append(nav);
      if(isDefine(did)){
         $(".search-nav").append(sep);
         $("#depart-input").find("span").eq(0).text(depart);
         $("#depart-input").find("span").eq(0).attr("code",dcode);
         $("#depart-input").find("span").eq(0).attr("id",did);
      }
      $(".s-nav").click(function(){
          goToBooks(scode,"","","",pageSize);
      })
  }
  if(isDefine(did)){
     getMajors(did);
      //添加院系的导航块
      var nav = $('<a href="#" class=""></a>');
      var sep = $('<span class="separate">&gt;</span>');
      nav.attr("class","d-nav");
      nav.text(depart);
      $(".search-nav").append(nav);
      if(isDefine(mid)){
         $(".search-nav").append(sep);
         $("#major-input").find("span").eq(0).text(major);
         $("#major-input").find("span").eq(0).attr("code",mcode);
         $("#major-input").find("span").eq(0).attr("id",mid);
      }
      $(".d-nav").click(function(){
           goToBooks(scode,dcode,"","",pageSize);
      })
  }
  if(isDefine(mid)){
      var nav = $('<a href="#" class=""></a>');
      var sep = $('<span class="separate">&gt;</span>');
      nav.attr("class","m-nav");
      nav.text(major);
      $(".search-nav").append(nav);
      if(isDefine(yearCode)){
          $(".search-nav").append(sep);
      }
      $(".m-nav").click(function(){
           goToBooks(scode,dcode,mcode,"",pageSize);
      })
  }

  if(isDefine(yearCode)){
      var nav = $('<a href="#" class=""></a>');
      nav.attr("class","y-nav");
      nav.attr("code",yearCode);
      nav.text(getYearCodeName(yearCode));
      $(".search-nav").append(nav);
  }

  
  
  var pageIndex = 0;
  var pageSize = 16;
  var pageT = 0;
  $(function(){
    $(".search-list-show").trigger("click");
    getBooks(scode,dcode,mcode,yearCode,pageSize);
    	       $(".pre-page").click(function(){
		        if(pageIndex == 0){
		            alert("当前已经是第一页");
		            return;
		        }
		        pageIndex--;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        });
		        getBooks(scode,dcode,mcode,yearCode,pageSize);
		    });
		    
		    $(".next-page").click(function(){
		        if(pageIndex == pageT-1){
		            alert("当前已经是最后一页");
		            return;
		        }
		        pageIndex++;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        })
		        getBooks(scode,dcode,mcode,yearCode,pageSize);
		   });
  })
  
  function goToBooks(scode,dcode,mcode,yearCode){
     var href = "searchschool.do?";
     if(isDefine(scode)){
        href += "school="+school+"&scode="+scode+"&sid="+sid;
     }
     if(isDefine(dcode)){
         href += "&depart="+depart+"&dcode="+dcode+"&did="+did;
     }
     if(isDefine(mcode)){
         href += "&major="+major+"&mcode="+mcode+"&mid="+mid;
     }
     if(isDefine(yearCode)){
         href += "&yearCode="+yearCode;
     }
    
     location.href=href;
  }
  
  function getBooks(scode,dcode,mcode,yearCode,pageSize){
     $(".page-wrapper").hide();
     var query = "pageIndex="+pageIndex+"&pageSize="+pageSize;
     if(isDefine(scode)){
        query += "&schoolCode="+scode;
     }
     if(isDefine(dcode)){
         query += "&departmentCode="+dcode;
     }
     if(isDefine(mcode)){
         query += "&majorCode="+mcode;
     }
     if(isDefine(yearCode)){
         query += "&yearCode="+yearCode;
     }
    //热门图书
	$(".type-book").remove();
	    request("listBook.do",query,function(data){
		var d = data.data;
		if(d.length < 1){
			alert("此书暂未收藏，请与管理员联系");
		}
		for(var i in d){
			var row = $('<div class="type-book"><div class="book-content"><a target="_blank"><img/></a>'
			+' <div class="book-name"><a target="_blank"></a></div><div class="book-price"><span class="now-price"></span>'
			+'<span class="ori-price"></span></div></div></div>');
            var book = d[i];
            row.find(".book-content a").attr("href","bookdetail.do?id="+book.id);
            row.find(".book-content a img").attr("src",getImg(book.img));
            row.find(".book-name a").text(book.name);
            row.find(".book-name a").attr("href","bookdetail.do?id="+book.id);
            row.find(".now-price").text("￥"+book.priceSelling);
            row.find(".ori-price").text("￥"+book.priceOriginal);
            $(".book-types-content-books").append(row);
		}
		var total = data.total;
		//计算总页数
		pageT = Math.ceil(total / pageSize);
		//对分页进行处理
		if(pageT > 1){
		    $(".page-wrapper").show();
		    $(".page-num").remove();
		    for(var i = 1 ; i <= pageT ; i++){
		        var row = $('<a class="page-num" href="#"></a>');
		        row.text(i);
		        row.attr("name","page-"+(i-1));
		        row.insertBefore($(".next-page"));
		        if(pageIndex == (i-1)){
		           row.addClass("now");
		        }
		    }
		    $(".page-num").click(function(){
		            if(!$(this).hasClass("now")){
		                pageIndex = parseInt($(this).text())-1;
		                getBooks(scode,dcode,mcode,yearCode,pageSize);
		            }
		    });
		}else{
		    $(".page-wrapper").hide();
		}
	});
  }
  
  </script>
</html>